package com.macro.mall.product.controller;

import com.github.pagehelper.PageInfo;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.PmPrptyPackage;
import com.macro.mall.product.dto.PmPrptyPackageDto;
import com.macro.mall.product.service.PmPrptyPackageService;
import com.macro.mall.product.util.DozerUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.net.URLDecoder;
import java.util.List;

/**
 * 属性管理Controller
 * Created by macro on 2018/4/26.
 */
@Controller
@Api(tags = "PmPrptyPackageController", description = "品类包管理")
@RequestMapping("/prptypackage")
public class PmPrptyPackageController {

    @Autowired
    private PmPrptyPackageService pmPrptyPackageService;
    @Autowired
    private Mapper mapper;

    @ApiOperation(value = "分页获取属性列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<PmPrptyPackageDto>> list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                            @RequestParam(value = "packageName", defaultValue = "") String packageName
    ) {
        PmPrptyPackageDto packageDto = new PmPrptyPackageDto();
        packageName= URLDecoder.decode(packageName);
        packageDto.setName(packageName);
        List<PmPrptyPackage> packageList = pmPrptyPackageService.listPage(pageNum, pageSize,packageDto);
        PageInfo<PmPrptyPackage> pageInfo = new PageInfo<PmPrptyPackage>(packageList);

        List<PmPrptyPackageDto> packageDtoList = DozerUtils.mapList(mapper, packageList, PmPrptyPackageDto.class);

        CommonPage<PmPrptyPackageDto> result = new CommonPage<PmPrptyPackageDto>();
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setTotal(pageInfo.getTotal());
        result.setTotalPage(pageInfo.getPages());
        result.setList(packageDtoList);

        return CommonResult.success(result);
    }

    @ApiOperation(value = "保存品类包")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult save(@Validated @RequestBody PmPrptyPackageDto packageDto, BindingResult result) {
        CommonResult commonResult;
        int count = pmPrptyPackageService.save(packageDto);
        if (count == 1) {
            commonResult = CommonResult.success(count);
        } else {
            commonResult = CommonResult.failed();
        }
        return commonResult;
    }
    @ApiOperation(value = "删除品类包")
    @RequestMapping(value = "/deletePrptyPackage", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult deletePrptyPackage(@RequestParam(value = "packageIds") String packageIds) {
        CommonResult commonResult;
        int count = pmPrptyPackageService.deletePackage(packageIds);
        if (count >0) {
            commonResult = CommonResult.success(count);
        } else {
            commonResult = CommonResult.failed();
        }
        return commonResult;
    }

    @ApiOperation(value = "获取单个品类包")
    @RequestMapping(value = "/getPackageById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmPrptyPackageDto> getPackageById(@RequestParam(value = "packageId", defaultValue = "") Long packageId
    ) {
        PmPrptyPackage prptyPackage = pmPrptyPackageService.getById(packageId);
        PmPrptyPackageDto packageDto= mapper.map(prptyPackage,PmPrptyPackageDto.class);
        return CommonResult.success(packageDto);
    }
}
